package com.yjq.programmer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjq.programmer.bean.CodeMsg;
import com.yjq.programmer.dao.LyricsMapper;
import com.yjq.programmer.domain.Lyrics;
import com.yjq.programmer.domain.LyricsExample;
import com.yjq.programmer.dto.LyricsDTO;
import com.yjq.programmer.dto.PageDTO;
import com.yjq.programmer.dto.ResponseDTO;
import com.yjq.programmer.service.ILyricsService;
import com.yjq.programmer.utils.CommonUtil;
import com.yjq.programmer.utils.CopyUtil;
import com.yjq.programmer.utils.UuidUtil;
import com.yjq.programmer.utils.ValidateEntityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨杨吖
 * @QQ 823208782
 * @WX yjqi12345678
 * @create 2022-11-13 17:49
 */
@Service
@Transactional
public class LyricsServiceImpl implements ILyricsService {

    @Resource
    private LyricsMapper lyricsMapper;

    /**
     * 分页获取歌词数据
     * @param pageDTO
     * @return
     */
    @Override
    public ResponseDTO<PageDTO<LyricsDTO>> getLyricsListByPage(PageDTO<LyricsDTO> pageDTO) {
        LyricsExample lyricsExample = new LyricsExample();
        // 判断是否进行关键字搜索
        LyricsDTO searchLyricsDTO = pageDTO.getSearchEntity();
        LyricsExample.Criteria criteria = lyricsExample.createCriteria();
        if(!CommonUtil.isEmpty(searchLyricsDTO.getContent())) {
            criteria.andContentLike("%" + searchLyricsDTO.getContent() + "%");
        }
        if(!CommonUtil.isEmpty(searchLyricsDTO.getMusicId())) {
            criteria.andMusicIdEqualTo(searchLyricsDTO.getMusicId());
        }
        // 不知道当前页多少，默认为第一页
        if(pageDTO.getPage() == null){
            pageDTO.setPage(1);
        }
        if(pageDTO.getSize() == null) {
            pageDTO.setSize(5);
        }
        lyricsExample.setOrderByClause("minute_time, second_time");
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        // 分页查出歌词数据
        List<Lyrics> lyricsList = lyricsMapper.selectByExample(lyricsExample);
        PageInfo<Lyrics> pageInfo = new PageInfo<>(lyricsList);
        // 获取数据的总数
        pageDTO.setTotal(pageInfo.getTotal());
        // 将domain类型数据  转成 DTO类型数据
        List<LyricsDTO> lyricsDTOList = CopyUtil.copyList(lyricsList, LyricsDTO.class);
        pageDTO.setList(lyricsDTOList);
        return ResponseDTO.success(pageDTO);
    }

    /**
     * 保存歌词数据(添加、修改)
     * @param lyricsDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> saveLyrics(LyricsDTO lyricsDTO) {
        // 进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(lyricsDTO);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDTO.errorByMsg(validate);
        }
        Lyrics lyrics = CopyUtil.copy(lyricsDTO, Lyrics.class);
        if(CommonUtil.isEmpty(lyrics.getId())){
            // id为空 说明是添加数据
            // 判断此分此秒歌词是否存在
            if(isLyricsContentExist(lyrics, "")){
                return ResponseDTO.errorByMsg(CodeMsg.LYRICS_CONTENT_EXIST);
            }
            // 生成8位id
            lyrics.setId(UuidUtil.getShortUuid());
            // 添加数据到数据库
            if(lyricsMapper.insertSelective(lyrics) == 0){
                return ResponseDTO.errorByMsg(CodeMsg.LYRICS_ADD_ERROR);
            }
        }else{
            // id不为空 说明是修改数据
            // 判断此分此秒歌词是否存在
            if(isLyricsContentExist(lyrics, lyrics.getId())){
                return ResponseDTO.errorByMsg(CodeMsg.LYRICS_CONTENT_EXIST);
            }
            // 修改数据库中数据
            if(lyricsMapper.updateByPrimaryKeySelective(lyrics) == 0){
                return ResponseDTO.errorByMsg(CodeMsg.LYRICS_EDIT_ERROR);
            }
        }
        return ResponseDTO.successByMsg(true, "保存成功！");
    }

    /**
     * 删除歌词数据
     * @param lyricsDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> removeLyrics(LyricsDTO lyricsDTO) {
        if(CommonUtil.isEmpty(lyricsDTO.getId())){
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 删除歌词信息
        if(lyricsMapper.deleteByPrimaryKey(lyricsDTO.getId()) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.LYRICS_DELETE_ERROR);
        }
        return ResponseDTO.successByMsg(true, "删除成功！");
    }

    /**
     * 根据音乐id获取歌词数据
     * @param lyricsDTO
     * @return
     */
    @Override
    public ResponseDTO<List<LyricsDTO>> getLyricsListByMusicId(LyricsDTO lyricsDTO) {
        if(CommonUtil.isEmpty(lyricsDTO.getMusicId())) {
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        LyricsExample lyricsExample = new LyricsExample();
        lyricsExample.setOrderByClause("minute_time, second_time");
        lyricsExample.createCriteria().andMusicIdEqualTo(lyricsDTO.getMusicId());
        List<Lyrics> lyricsList = lyricsMapper.selectByExample(lyricsExample);
        return ResponseDTO.success(CopyUtil.copyList(lyricsList, LyricsDTO.class));
    }


    /**
     * 判断此分此秒歌词是否存在
     * @param lyrics
     * @param id
     * @return
     */
    public Boolean isLyricsContentExist(Lyrics lyrics, String id) {
        LyricsExample lyricsExample = new LyricsExample();
        lyricsExample.createCriteria().andMinuteTimeEqualTo(lyrics.getMinuteTime()).andSecondTimeEqualTo(lyrics.getSecondTime()).andMusicIdEqualTo(lyrics.getMusicId());
        List<Lyrics> selectedLyricsList = lyricsMapper.selectByExample(lyricsExample);
        if(selectedLyricsList != null && selectedLyricsList.size() > 0) {
            if(selectedLyricsList.size() > 1){
                return true; //出现重名
            }
            if(!selectedLyricsList.get(0).getId().equals(id)) {
                return true; //出现重名
            }
        }
        return false;//没有重名
    }
}
